import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
  path: '/login',
  name: 'login',
  component: () => import('@/views/Login.vue')
},
{
  path: '/',
  component: () => import('@/views/Home.vue'),
  children: [{
    path: '/',
    name: 'home',
    component: () => import('@/views/Welcome.vue')
  },
  {
    path: '/customers',
    name: 'Customers',
    component: () => import('@/views/crm/Customers.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/customers/:id',
    name: 'CustomerDetail',
    component: () => import('@/views/crm/CustomerDetail.vue')
  },
  {
    path: '/users',
    name: 'Users',
    component: () => import('@/views/sys/Users.vue')
  }
  ]
}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
